import { _decorator, Component, LabelShadow, LightProbeGroup, log, Material, Node, resources, SpriteFrame, SpriteRenderer, tween, v3 } from 'cc';
import { GameManager } from './Manager/GameManager';
import { Label3D } from './Label3D';
const { ccclass, property } = _decorator;

@ccclass('Card')
export class Card extends Component {

    CardBack: Node = null;
    CardTop: Node = null;
    CardRole: Node = null;
    CardTag: Node = null;
    Price: Node = null;
    PriceValue: number = 0;
    Name: string = "";

    CardPosed = v3();
    CardAngled = v3();

    CardBase: Node = null;
    IsBoss: boolean = false;

    onLoad() {
        this.CardBack = this.node.getChildByName("Card").getChildByName("CardBack");
        this.CardTop = this.node.getChildByName("Card").getChildByName("CardTop");
        this.CardRole = this.node.getChildByName("Card").getChildByName("CardRole");
        this.CardTag = this.node.getChildByName("Card").getChildByName("CardTag");
        this.Price = this.CardTop.getChildByName("Price");
    }

    InitCardPrice() {//初始化卡牌价格
        for (let i = 0; i < this.PriceValue.toString().length; i++) {
            let numberNd = new Node();
            numberNd.setParent(this.Price);
            switch (this.PriceValue.toString().length) {
                case 2:
                    {
                        numberNd.setPosition(-0.15 + i * 0.3, 0);
                    }
                    break;
                case 3:
                    {
                        numberNd.setPosition(-0.3 + i * 0.3, 0);
                    }
                    break;
            }
            numberNd.addComponent(SpriteRenderer);
            let material = this.CardTop.getComponent(SpriteRenderer).getMaterial(0);
            numberNd.getComponent(SpriteRenderer).setMaterial(material, 0);

            let singleStr = this.PriceValue.toString().substring(i, i + 1);
            resources.load("Sprites/Common/" + singleStr + "/spriteFrame", SpriteFrame, (err, spf: SpriteFrame) => {
                numberNd.getComponent(SpriteRenderer).spriteFrame = spf;
            });
        }
    }

    InitCardBack() {//初始化卡牌背景
        if (this.Name.substring(0, 2) == "黄金") {
            resources.load("Texture/CardBack/CardBack2/spriteFrame", SpriteFrame, (err, spf) => {
                this.CardBack.getComponent(SpriteRenderer).spriteFrame = spf;
            })
        }
        else {
            if (this.Name.substring(0, 2) == "钻石") {
                resources.load("Texture/CardBack/CardBack3/spriteFrame", SpriteFrame, (err, spf) => {
                    this.CardBack.getComponent(SpriteRenderer).spriteFrame = spf;
                })
            }
            else {
                if (this.PriceValue >= 100 && this.PriceValue < 200) {
                    resources.load("Texture/CardBack/CardBack1/spriteFrame", SpriteFrame, (err, spf) => {
                        this.CardBack.getComponent(SpriteRenderer).spriteFrame = spf;
                    })
                }
                if (this.PriceValue >= 200 && this.PriceValue < 300) {
                    resources.load("Texture/CardBack/CardBack2/spriteFrame", SpriteFrame, (err, spf) => {
                        this.CardBack.getComponent(SpriteRenderer).spriteFrame = spf;
                    })
                }
                if (this.PriceValue >= 300) {
                    resources.load("Texture/CardBack/CardBack3/spriteFrame", SpriteFrame, (err, spf) => {
                        this.CardBack.getComponent(SpriteRenderer).spriteFrame = spf;
                    })
                }
            }
        }
    }

    InitCardRole() {//初始化卡牌人物
        resources.load("Texture/CardRole/" + this.Name + "/spriteFrame", SpriteFrame, (err, spf) => {
            this.CardRole.getComponent(SpriteRenderer).spriteFrame = spf;
        })
    }

    start() {
        if (!GameManager.Instance.IsEnterFight) {
            this.node.scale = v3(1.7, 1.7, 1.7);
            tween(this.node)
                .to(3, { eulerAngles: v3(0, 10, 0) }, { easing: "quadInOut" })
                .to(3, { eulerAngles: v3(0, -10, 0) }, { easing: "quadInOut" })
                .union()
                .repeatForever()
                .start();
            this.Name = GameManager.Instance.LevelData.json[GameManager.Instance.GetLocalStorage("Level")]["Card" + this.node.parent.getSiblingIndex()];
            if (this.IsBoss) {
                this.PriceValue = GameManager.Instance.MonsterData.json[this.Name] * 2 + 60;
            }
            else {
                this.PriceValue = GameManager.Instance.MonsterData.json[this.Name];
            }
            this.CardTag.getComponentInChildren(Label3D).string = this.Name;//初始化卡牌名称
            this.UpdateCardTop();
            this.InitCardPrice();
            this.InitCardBack();
            this.InitCardRole();
            if (this.Name == "绿兽" || this.Name == "神蜥") {
                this.CardTop.getChildByName("Video").active = true;
                this.Price.active = false;
            }
        }
        else {
            if (this.IsBoss) {
                this.PriceValue = GameManager.Instance.MonsterData.json[this.Name] * 2 + 60;
            }
            else {
                this.PriceValue = GameManager.Instance.MonsterData.json[this.Name];
            }
            this.CardTag.getComponentInChildren(Label3D).string = this.Name;//初始化卡牌名称
            this.InitCardPrice();
            this.InitCardBack();
            this.InitCardRole();
        }

    }

    update(deltaTime: number) {

    }

    UpdateCardTop() {//更新卡牌价格底框
        if (this.Name == "绿兽" || this.Name == "神蜥") return;
        if (GameManager.Instance.MoneyNum < this.PriceValue) {
            if (this.CardTop.getComponent(SpriteRenderer).spriteFrame.name != "RedCardTop") {
                resources.load("Texture/RedCardTop/spriteFrame", SpriteFrame, (err, spf) => {
                    this.CardTop.getComponent(SpriteRenderer).spriteFrame = spf;
                })
            }
        }
        else {
            if (this.CardTop.getComponent(SpriteRenderer).spriteFrame.name != "GreenCardTop") {
                resources.load("Texture/GreenCardTop/spriteFrame", SpriteFrame, (err, spf) => {
                    this.CardTop.getComponent(SpriteRenderer).spriteFrame = spf;
                })
            }
        }
    }
}


